/*--------------------------------------------------------------------------------------*\
**
** adv_ferry.cpp
**
** Heroes IV
** Copyright 2000, The 3DO Company
**
\*--------------------------------------------------------------------------------------*/

// kill the 255+ character identifier warning.  Templates tend to exceed that easily.
#include "precompiled.h"
#pragma warning( disable: 4786 )

#include "adventure_events_update.h"

#include "adventure_map.h"
#include "adventure_object.h"
#include "adventure_object_ptr.h"
#include "compressed_filter.h"
#include "game_time.h"

#include "saved_game_header.h"

namespace 
{
	const int k_memory_event_buffer_size	= 0;
	const int k_event_lifespan				= 0*1000;			//in mili-seconds
};

t_adventure_event_update::t_adventure_event_update ( ) 
: t_adventure_event_base ( k_update_event ) 
{ 	
	m_event_buffer_state_a	= new t_memory_buffer_counted ( k_memory_event_buffer_size ) ;
	m_event_buffer_state_b	= new t_memory_buffer_counted ( k_memory_event_buffer_size ) ;
}

t_adventure_event_update::t_adventure_event_update ( t_adventure_object* adv_object, t_level_map_point_2d point
													, t_memory_buffer_counted_ptr adv_buffer_pre_update ) 
: t_adventure_event_base ( k_update_event ) 
{ 	
	m_global_id				= adv_object->get_global_id();
	m_event_buffer_state_a	= adv_buffer_pre_update->clone();
	m_event_buffer_state_b	= t_adventure_event_functions::create_state_cahce_buffer(adv_object);
	m_event_point			= point;
}

void t_adventure_event_update::execute_event( t_adventure_map* map, t_saved_game_header const & header ) 
{	
	t_adventure_object * object = map->get_adventure_object_with_gid ( m_global_id );
	
	if (object == NULL) 
		return;

	t_adventure_event_functions::read_object_from_state_cache (object, m_event_buffer_state_b );

	m_time_end = get_time() + k_event_lifespan;

	return;
}

void t_adventure_event_update::undo_event( t_adventure_map* map , t_saved_game_header const & header )
{
	t_adventure_object * object = map->get_adventure_object_with_gid ( m_global_id );
		
	if (object == NULL) 
		return;

	t_adventure_event_functions::read_object_from_state_cache (object, m_event_buffer_state_a );

	return;
}

bool t_adventure_event_update::read( std::streambuf & buffer , int version )
{
	m_event_buffer_state_a->clear_buffer();
	m_event_buffer_state_b->clear_buffer();

	//read buffer a///////////////////////////////////////////////////
	int event_buffer_flag_a  = 	get<t_uint32> ( buffer );

	if (event_buffer_flag_a != 0)
	{
		m_event_buffer_state_a->read( buffer );
	}

	//read buffer b///////////////////////////////////////////////////
	int event_buffer_flag_b  = 	get<t_uint32> ( buffer );

	if (event_buffer_flag_b != 0)
	{
		m_event_buffer_state_b->read( buffer );
	}

	return t_adventure_event_base::read( buffer , version );
}

bool t_adventure_event_update::write( std::streambuf & buffer )
{
	//write buffer a///////////////////////////////////////////////////
	if (m_event_buffer_state_a.get() != 0)
	{
		put<t_uint32> (buffer, 1);

		m_event_buffer_state_a->write( buffer );
	}
	else
		put<t_uint32> (buffer, 0);
	
	//write buffer b///////////////////////////////////////////////////
	if (m_event_buffer_state_b.get() != 0)
	{
		put<t_uint32> (buffer, 1);
		
		m_event_buffer_state_b->write( buffer );
	}
	else
		put<t_uint32> (buffer, 0);
	
	return t_adventure_event_base::write( buffer );
}
